<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>todolist</title>
</head>

<body>
    <div id="app">
        <input type="text" v-model="inputVal" />
        <button @click="btnClick">submit</button>
        <ul>
            <todo-item 
            v-for="(content,index) in todos" 
            :key="index" 
            :content="content"
            :index="index"
            @delcomp="delcomp">
            </todo-item>
        </ul>
    </div>
    <script>

        // 注册一个全局vue组件
        Vue.component("todo-item", {
            props: ['content','index'],
            template: "<li @click='delcomp'>{{content}}</li>",
            methods:{
                delcomp: function(index){
                    // alert(this.index);
                    this.$emit("delcomp","test")
                }
            }
        })

        var app = new Vue({
            el: "#app",
            data: {
                inputVal: '',
                todos: []
            },
            methods: {
                btnClick: function () {
                    this.todos.push(this.inputVal);
                    this.inputVal = ''
                },
                delcomp:function(index) {
                    // alert(index);
                    this.todos.splice(index,1)
                }
            }
        })
    </script>
</body>

</html>